package com.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MyDateUtil {
	public static SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat sfMD=new SimpleDateFormat("M/d");
	public static SimpleDateFormat sfh=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat sfm=new SimpleDateFormat("yyyy-MM");
	public static SimpleDateFormat sfy=new SimpleDateFormat("yyyy");
	public static SimpleDateFormat sfhz=new SimpleDateFormat("yyyy年MM月");
	public static SimpleDateFormat time=new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat txtName=new SimpleDateFormat("yyMMdd");
	public static SimpleDateFormat fileName=new SimpleDateFormat("yyMMddHHmmss");
	public static SimpleDateFormat csj=new SimpleDateFormat("yyyyMMddHHmmssSSS");
	public static SimpleDateFormat df = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.UK);
	
	public static Integer getMonthDay(String month){
		Calendar rightNow=Calendar.getInstance(); 
		try {
			rightNow.setTime(sfm.parse(month));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int days = rightNow.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days;
	}
	
	public static Date getYearDate(Date date, Integer year, Integer month, Integer day){
		Calendar rightNow=Calendar.getInstance(); 
		rightNow.setTime(date);
		rightNow.add(Calendar.YEAR, year);
		rightNow.add(Calendar.MONTH, month);
		rightNow.add(Calendar.DATE, day);
		return rightNow.getTime();
	}
	
	public static boolean getCompareDate(String value) {
		try {
			Date parse1 = sf.parse(value);
			String begin = txtName.format(parse1);
			String end = txtName.format(new Date());
			if(Integer.parseInt(begin) > Integer.parseInt(end)) {
				return true;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
	/***
	 * 获取当前时间
	 * 1.yyyy-MM-dd 
	 * 2.yyyy-MM-dd HH:mm:ss
	 * 3.yyyyMMdd
	 * @return
	 */
	public static String getStrCurrentDate(int mark){
		String format = null;
		if(mark == 1) {
			Date date = new Date();
			format = sf.format(date);
		}else if(mark == 2){
			Date date = new Date();
			format = sfh.format(date);
		}else if(mark == 3){
			Date date = new Date();
			format = txtName.format(date);
		}
		return format;
	}
	/***
	 * 转换时间
	 * 1.yyyy-MM-dd 
	 * 2.yyyy-MM-dd HH:mm:ss
	 * @return
	 * @throws ParseException 
	 */
	public static Date getDate(String str,int mark) throws ParseException{
		Date date = null;
		if(mark == 1) {
			date = sf.parse(str);
		}else if(mark == 2){
			date = sfh.parse(str);
		}
		return date;
	}
	/**
	  *  获取当前时间Date()
	 * @return
	 */
	public static Date getDate(){
		return new Date();
	} 
	
	/**Date和String，二选一其中一个为null
	 * 参数三：
	 * 1.yyyy-MM-dd 
	 * 2.yyyy-MM-dd HH:mm:ss
	 * 3.yyMMdd
	 * 4.yyMMddHHmmss
	 * 5.yyyyMMddHHmmssSSS
	 * @return
	 * @throws ParseException
	 */
	public static Long getDateCount(Date d, String s,int mark) throws ParseException {
		if(d == null) {
			if(mark == 1) {
				d = sf.parse(s);
			}else if(mark == 2){
				d = sfh.parse(s);
			}else if(mark == 3){
				d = txtName.parse(s);
			}else if(mark == 4){
				d = fileName.parse(s);
			}else if(mark == 5){
				d = csj.parse(s);
			}
		}
		return d.getTime();
	}
	/**
	 * 根据出生日期计算年龄
	 * @param brithday
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public static int getCurrentAgeByBirthdate(String brithday) throws Exception {
		try{
			Calendar calendar = Calendar.getInstance();
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			String currentTime = formatDate.format(calendar.getTime());
			Date today = formatDate.parse(currentTime);
			Date brithDay = formatDate.parse(brithday);
			return today.getYear() - brithDay.getYear();
		}catch(Exception e){
			return 0;
		}
	}
	/**
	 * 计算两个日期相隔的天数  
	 * @param firstString
	 * @param secondString
	 * @return
	 */
    public static int nDaysBetweenTwoDate(String firstString, String secondString) {  
        Date firstDate = null;  
        Date secondDate = null;  
        try {  
            secondDate = sf.parse(secondString);  
            firstDate = sf.parse(firstString);  
        } catch (Exception e) {  
            // 日期型字符串格式错误  
            System.out.println("日期型字符串格式错误");  
        }  
        int nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));  
        return nDay;  
    }

}
